package com.wgchao.authmanager.domain.model;

import com.wgchao.authmanager.domain.params.AdministratorCreator;
import com.wgchao.authmanager.domain.params.AdministratorUpdator;
import com.wgchao.authmanager.infrastructure.exception.ObjectDisabledException;
import com.wgchao.authmanager.infrastructure.exception.ObjectNotMatchingException;
import lombok.Getter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Date;

/**
 * @作者：wgchao
 * @时间：20:38 2020/4/25 0025
 * @用途：管理员
 **/
@Getter
public class Administrator extends DomainIdentify implements LoginAdmin{
    //全局标识
    private String uname;
    private String password;
    private String name;
    private String agencyCode;
    private String tel;
    private String email;
    private boolean disabled;
    private Operator creator;
    private Operator lastupdator;

    protected Administrator(String uname, String password, String name, String agencyCode,
                         String tel, String email, boolean disabled,
                         Operator creator, Operator lastupdator) {
        this.uname = uname;
        this.password = password;
        this.name = name;
        this.agencyCode = agencyCode;
        this.tel = tel;
        this.email = email;
        this.disabled = disabled;
        this.creator = creator;
        this.lastupdator = lastupdator;
    }

    public Administrator(Long id, String uname, String password, String name, String agencyCode,
                            String tel, String email, boolean disabled,
                            Operator creator, Operator lastupdator, Long version) {
        setId(id);
        this.uname = uname;
        this.password = password;
        this.name = name;
        this.agencyCode = agencyCode;
        this.tel = tel;
        this.email = email;
        this.disabled = disabled;
        this.creator = creator;
        this.lastupdator = lastupdator;
        setVersion(version);
    }

    public static Administrator addAdmin(AdministratorCreator creator, String pwd, Agency agency, String operator){
        if(!agency.normal()){
            throw new ObjectDisabledException("所在机构不可用");
        }

        return new Administrator(creator.getUname(), pwd, creator.getName(), agency.getAgencyCode(),
                creator.getTel(), creator.getEmail(), false,
                new Operator(operator, new Date()), null);
    }

    public boolean normal() {
        return !this.disabled;
    }

    public void resiveInfo(AdministratorUpdator updator, String operator){
        if(!normal()){
            throw new ObjectDisabledException("账户不可用");
        }
        if(updator.getName() != null){
            this.name = updator.getName().orElse(null);
        }
        if(updator.getTel() != null){
            this.tel = updator.getTel().orElse(null);
        }
        if(updator.getEmail() != null){
            this.email = updator.getEmail().orElse(null);
        }
        this.lastupdator = new Operator(operator, new Date());
    }

    public void stopUsing(String operator){
        this.disabled = true;
        this.lastupdator = new Operator(operator, new Date());
    }

    public void reEnable(String operator){
        this.disabled = false;
        this.lastupdator = new Operator(operator, new Date());
    }

    public void assertBelongTo(Agency agency){
        if(!this.agencyCode.equals(agency.getAgencyCode())){
            throw new ObjectNotMatchingException("账户"+this.uname+"不属于机构"+agency.getAgencyName());
        }
    }

    /**
     * 密码匹配
     * @param pwd 待验证的密码
     * @return
     */
    @Override
    public boolean authentication(String pwd) {
        return new BCryptPasswordEncoder().matches(pwd, this.password);
    }

    @Override
    public LoginAdminInfo getInfo() {
        return new LoginAdminInfo(this.uname, this.agencyCode, findId());
    }
}
